import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import http from '../plugins/http'

//默认会访问index，如果你不加文件名
import store from '../store'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import( /* webpackChunkName: "about" */ '../views/login.vue')
  },
  {
    path: '/brand/list',
    component: () => import( /* webpackChunkName: "about" */ '../views/brand/list.vue')
  },
  {
    path: '/brand/create',
    component: () => import( /* webpackChunkName: "about" */ '../views/brand/save.vue')
  },
  {
    path: '/cate/list',
    component: () => import( /* webpackChunkName: "about" */ '../views/cate/list.vue')
  },
  {
    path: '/cate/create',
    component: () => import( /* webpackChunkName: "about" */ '../views/cate/save.vue')
  },
  {
    path: '/cate/edit/:id',
    component: () => import( /* webpackChunkName: "about" */ '../views/cate/save.vue')
  },
  {
    path: '/product/list',
    component: () => import( /* webpackChunkName: "about" */ '../views/product/list.vue')
  },
  {
    path: '/product/create',
    component: () => import( /* webpackChunkName: "about" */ '../views/product/save.vue')
  },
  {
    path: '/product/edit/:id',
    component: () => import( /* webpackChunkName: "about" */ '../views/product/save.vue')
  },
  {
    path: '/user/list',
    component: () => import( /* webpackChunkName: "about" */ '../views/user/list.vue')
  },
  {
    path: '/user/create',
    component: () => import( /* webpackChunkName: "about" */ '../views/user/save.vue')
  },
  {
    path: '/user/edit/:id',
    component: () => import( /* webpackChunkName: "about" */ '../views/user/save.vue')
  },
  {
    path: '/user/permission/:id',
    component: () => import( /* webpackChunkName: "about" */ '../views/user/permission.vue')
  },
  {
    path: '/role/list',
    component: () => import( /* webpackChunkName: "about" */ '../views/role/list.vue')
  },
  {
    path: '/role/create',
    component: () => import( /* webpackChunkName: "about" */ '../views/role/save.vue')
  },
  {
    path: '/role/edit/:id',
    component: () => import( /* webpackChunkName: "about" */ '../views/role/save.vue')
  },
  {
    path: '/role/permission/:id',
    component: () => import( /* webpackChunkName: "about" */ '../views/role/permission.vue')
  },
  {
    path: '/data/sale',
    component: () => import( /* webpackChunkName: "about" */ '../views/data.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//老师后端 192.168.0.165
router.beforeEach((to, from, next) => {

  //如果是去登录，就不用校验，直接通过
  if (to.path == '/login') {
    next()
  } else {
    //如果已经登录，并且用户信息已经存到公用state，就直接通过
    if (store.state.user.id) {
      next()
    } else {
      //否则调获取用户信息接口，把用户信息保存到state
      http.get('/user/info').then(res => {
        // res = res.data
        if (res.errcode == 0) {
          store.commit('setUser', res.data)
          next() //跳转页面
        }else{
          router.push('/login')
        }
      })
    }

  }

})

export default router